using System;
using System.Collections;
using System.Drawing;
using System.IO;

namespace PhotoBlenderLib
{
	/// <summary>
	/// Photo
	/// </summary>
	public class Photo : IDisposable
	{

		#region Private Fields

		private string path = string.Empty;
		private Bitmap bitmap;

		#endregion


		#region Public Methods
		
		public Photo(string FilePath) 
		{
			path = FilePath;
			if(!File.Exists(path))
				throw new System.IO.FileNotFoundException("File not found.", path);
		}


		public void Dispose() {
			if(bitmap != null)
				bitmap.Dispose();
		}

		#endregion


		#region Private Methods

		private void LoadImage() {
			if (!File.Exists(path))
				throw new FileNotFoundException("File not found.", path);

			bitmap = Bitmap.FromFile(path) as Bitmap;
		}

		#endregion


		#region Public Properties

		public string Path {
			get { return path; }
			set { path = value; }
		}

		public Bitmap Image {
			get {
				if(bitmap == null) 
					LoadImage();
				
				return bitmap;
			}
		}

		#endregion
	}

	/// <summary>
	/// Photo Collection
	/// </summary>
	public class PhotoCollection : CollectionBase
	{
		public PhotoCollection (){}
		
		public Photo this[int index] {
			get { return (Photo)List[index]; }
			set { List[index] = value; }
		}

		public void Add(Photo photo) {
			this.List.Add(photo);
		}

		public bool Contains(Photo photo) {
			return this.List.Contains(photo);
		}

		public int IndexOf(Photo photo) {
			return List.IndexOf(photo);
		}

		public void Insert(int index, Photo photo) {
			List.Insert(index, photo);
		}

		public void Remove(Photo photo) {
			this.List.Remove(photo);
		}

		protected void OnRemoveComplete(int index, Photo value) {
			value.Dispose();
		}
	
	}


	/// <summary>
	/// Photo Factory
	/// </summary>
	public class PhotoFactory 
	{
		public static PhotoCollection CreatePhotoCollection(IEnumerable photoPaths) 
		{
			PhotoCollection photos = new PhotoCollection();
			
			foreach(string s in photoPaths) {
                if (isImage(s))
				    photos.Add(new Photo(s));
			}

			return photos;
		}

        private static bool isImage(string path)
        {
            if (path.EndsWith(".jpg") ||
                path.EndsWith(".png") ||
                path.EndsWith(".bmp") ||
                path.EndsWith(".gif"))
            {
                return true;
            }
            return false;
        }
	}
}
